<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="专辑ID" prop="albumId">
        <el-input
          v-model="queryParams.albumId"
          placeholder="请输入专辑ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="歌名" prop="songName">
        <el-input
          v-model="queryParams.songName"
          placeholder="请输入歌名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="歌手" prop="singer">
        <el-input
          v-model="queryParams.singer"
          placeholder="请输入歌手"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发行日期" prop="releaseDate">
        <el-date-picker
          clearable
          v-model="queryParams.releaseDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发行日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['song:song:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['song:song:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['song:song:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['song:song:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="songList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="音乐ID" align="center" prop="songId" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="专辑名" align="center" prop="albumName" />
      <el-table-column label="歌名" align="center" prop="songName" />
      <el-table-column label="图片" align="center" prop="imgUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="歌手" align="center" prop="singer" />
      <el-table-column label="歌词" align="center">
        <template #default="scope">
          <div class="lyric-ellipsis">{{ scope.row.lyric }}</div>
        </template>
      </el-table-column>

      <el-table-column
        label="发行日期"
        align="center"
        prop="releaseDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['song:song:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['song:song:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改音乐信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="专辑名" prop="albumName">
          <el-select v-model="form.albumName" placeholder="请选择专辑名">
            <el-option
              v-for="album in albumList"
              :key="album.albumId"
              :label="album.albumName"
              :value="album.albumName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="歌名" prop="songName">
          <el-input v-model="form.songName" placeholder="请输入歌名" />
        </el-form-item>
        <el-form-item label="歌手" prop="singer">
          <el-select v-model="form.singer" placeholder="请选择歌手">
            <el-option
              v-for="singer in singerList"
              :key="singer.singerId"
              :label="singer.singerName"
              :value="singer.singerName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="封面图片路径" prop="imgUrl">
          <image-upload v-model="form.imgUrl" />
        </el-form-item>
        <el-form-item label="音乐文件路径" prop="fileUrl">
          <file-upload v-model="form.fileUrl" />
        </el-form-item>
        <el-form-item label="歌词" prop="lyric">
          <el-input
            v-model="form.lyric"
            type="textarea"
            :rows="4"
            placeholder="请输入歌词"
          />
        </el-form-item>
        <el-form-item label="发行日期" prop="releaseDate">
          <el-date-picker
            clearable
            v-model="form.releaseDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发行日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择音乐分类">
            <el-option
              v-for="item in songTypeList"
              :key="item.categoryId"
              :label="item.categoryName"
              :value="item.categoryName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSong,
  getSong,
  delSong,
  addSong,
  updateSong,
} from "@/api/song/song";
import { listAlbum } from "@/api/album/album";
import { listSinger } from "@/api/singer/singer";
import { getSongTypeList } from "@/api/song_category/song_category";
export default {
  name: "Song",
  data() {
    return {
      songTypeList: [],
      albumList: [],
      singerList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 音乐信息表格数据
      songList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        songName: null,
        singer: null,
        releaseDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        songName: [
          { required: true, message: "歌名不能为空", trigger: "blur" },
        ],
        albumName: [
          { required: true, message: "专辑名不能为空", trigger: "change" },
        ],
        singer: [
          { required: true, message: "歌手不能为空", trigger: "change" },
        ],
        type: [
          { required: true, message: "音乐分类不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.listAlbum();
    this.listSinger();
    this.getSongTypeList();
  },
  methods: {
    getSongTypeList() {
      getSongTypeList().then((response) => {
        this.songTypeList = response;
        console.log("分类为：", this.songTypeList);
      });
    },
    // 获取专辑列表
    listAlbum() {
      listAlbum().then((response) => {
        this.albumList = response;
      });
    },
    // 获取歌手列表
    listSinger() {
      listSinger().then((response) => {
        this.singerList = response;
      });
    },
    /** 查询音乐信息列表 */
    getList() {
      this.loading = true;
      listSong(this.queryParams)
        .then((response) => {
          this.songList = response;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        songId: null,
        albumId: null,
        albumName: null,
        songName: null,
        imgUrl: null,
        fileUrl: null,
        singer: null,
        lyric: null,
        releaseDate: null,
        type: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        songName: null,
        singer: null,
        releaseDate: null,
      };
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.songId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加音乐信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const songId = row.songId || this.ids;
      getSong(songId).then((response) => {
        this.form = response.data;
        console.log("表格：", this.form);
        this.open = true;
        this.title = "修改音乐信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.songId != null) {
            updateSong(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSong(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const songIds = row.songId || this.ids;
      this.$modal
        .confirm('是否确认删除音乐信息编号为"' + songIds + '"的数据项？')
        .then(function () {
          return delSong(songIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "song/song/export",
        {
          ...this.queryParams,
        },
        `song_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.lyric-ellipsis {
  white-space: nowrap; /* 不换行 */
  overflow: hidden; /* 超出隐藏 */
  text-overflow: ellipsis; /* 显示省略号 */
  max-width: 200px; /* 控制最大宽度，根据实际情况调整 */
}
</style>
